<template>
  <div class="container">
    <van-nav-bar title="搜索中心" left-arrow @click-left="$router.back()" />
    <!-- 搜索框 -->
    <van-search
      v-model="keyWord"
      @search="onSearch"
      shape="round"
      placeholder="请输入搜索关键词"
    />
    <!-- 联想词展示 -->
    <van-cell-group class="suggest-box" v-if="keyWord">
      <!-- span高亮联想关键词 -->
      <van-cell
        @click="onSearch(sug.replace(`<span>${keyWord}</span>`, keyWord))"
        v-for="sug in suggestList"
        :key="sug"
        icon="search"
        ><p v-html="sug">ava</p></van-cell
      >
    </van-cell-group>
    <!-- 搜索历史记录 -->
    <div class="history-box" v-else-if="historyList.length">
      <div class="head">
        <span>历史记录</span>
        <van-icon @click="delAll" name="delete"></van-icon>
      </div>
      <van-cell-group>
        <van-cell v-for="history in historyList" :key="history">
          <a @click="onSearch(history)" class="word_btn">{{ history }}</a>
          <van-icon
            @click="delHistory(history)"
            class="close_btn"
            slot="right-icon"
            name="cross"
          />
        </van-cell>
      </van-cell-group>
    </div>
  </div>
</template>
<script>
import { suggest } from '../../api/article'
const TT_SEA = 'TT_SEA'
export default {
  name: 'search-index',
  data () {
    return {
      // 搜索关键词
      keyWord: '',
      // 联想词列表
      suggestList: [],
      // 搜索历史记录
      historyList: JSON.parse(localStorage.getItem(TT_SEA)) || []
    }
  },
  watch: {
    keyWord () {
      this.timer && clearTimeout(this.timer)
      // 防抖
      // this.timer:存储定时器任务的id，取消任务需要
      this.timer = setTimeout(async () => {
        // 执行联想补全
        // 联想补全
        if (!this.keyWord) return
        const { data } = await suggest(this.keyWord)
        this.suggestList = data.options.map((item) => item.toLowerCase().replace(this.keyWord, `<span>${this.keyWord}</span>`))
      }, 600)
    }
  },
  methods: {
    // 删除全部历史记录
    delAll () {
      this.historyList = []
      localStorage.setItem(TT_SEA, null)
    },
    // 删除历史记录
    delHistory (key) {
      // 内存删除
      const curIndex = this.historyList.findIndex((item) => item === key)
      this.historyList.splice(curIndex, 1)
      // 本地删除
      localStorage.setItem(TT_SEA, JSON.stringify(this.historyList))
    },
    onSearch (val) {
      // 存储用户搜索的关键词=>排重
      if (!this.historyList.includes(val)) {
        this.historyList.push(val)
      }
      // 本地同步
      localStorage.setItem(TT_SEA, JSON.stringify(this.historyList))
      // 跳转到搜索结果页面
      this.$router.push({ path: '/search/result', query: { q: val } })
    }
  }
}
</script>

<style scoped lang='less'>
// 联想词样式
.suggest-box {
  /deep/ .van-cell {
    padding: 10px 20px;
    color: #999;
    p {
      span {
        color: red;
      }
    }
  }
}
// 历史记录样式
.history-box {
  padding: 0 20px;
  .head {
    line-height: 36px;
    color: #999;
    text-align: left;
    .van-icon {
      font-size: 16px;
      float: right;
      margin-top: 10px;
    }
  }
  .van-cell {
    padding: 10px 0;
  }
  .word_btn {
    color: #3296fa;
  }
  .close_btn {
    margin-top: 5px;
    color: #999;
  }
}
</style>
